<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            height: 200px;
            width: 200px;
            position: absolute;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <div class="box">

    </div>
    <script>
        function DragBox(el) {
            this.el = document.querySelector(el);
        }
        DragBox.prototype = {
            constructor: DragBox,
            dragStar() {
                var self = this;
                this.el.onmousedown = function (evt) {
                    var e = evt || window.event;
                    var disX = e.pageX - this.offsetLeft;
                    var disY = e.pageY - this.offsetTop;
                    self.dragIng(disX, disY);
                    self.dragEnd()
                }

            },
            dragIng(x, y) {
                let self = this;
                document.onmousemove = function (evt) {
                    var e = evt || window.event;
                    self.el.style.left = e.pageX - x + "px";
                    self.el.style.top = e.pageY - y + "px";
                }
            },
            dragEnd() {
                document.onmouseup = function () {
                    document.onmousemove = document.onmouseup = null;
                }
            }
        }


        new DragBox(".box").dragStar()

    

    
    </script>
</body>

</html>